package com.wude.guild;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.mc.developmentkit.utils.MCUtils;
import com.mc.developmentkit.utils.ToastUtil;
import com.umeng.analytics.MobclickAgent;
import com.wude.bean.GiftDetBean;
import com.wude.bean.UserInfo;
import com.wude.guild.dialog.GiftDialog;
import com.wude.guild.servercore.mints.TitleBaseActivity;
import com.wude.http.HttpCom;
import com.wude.http.HttpUtils;
import com.wude.tools.Utils;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 礼包详情
 * Created by Administrator on 2017/4/8.
 */
@SuppressLint("HandlerLeak")
public class GiftDetActivity extends TitleBaseActivity {
    @BindView(R.id.icon)
    ImageView icon;
    @BindView(R.id.shuliang)
    TextView shuliang;
    @BindView(R.id.riqi)
    TextView riqi;
    @BindView(R.id.gift_cont)
    TextView giftCont;
    @BindView(R.id.shuming)
    TextView shuming;
    @BindView(R.id.qq)
    TextView qq;
    @BindView(R.id.duihuan)
    TextView duihuan;
    private String id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gift_det);
        ButterKnife.bind(this);

        id = getIntent().getStringExtra("id");
        HashMap<String, String> map = new HashMap<>();
        map.put("gift_id", id);
        HttpCom.POST(handler, HttpCom.getGiftDetURL(), map, false);
    }

    @OnClick({R.id.duihuan})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.duihuan:
                UserInfo loginUser = Utils.getLoginUser();
                if (loginUser == null) {
                    ToastUtil.showToast("请先登录");
                    return;
                }
                HashMap<String, String> map = new HashMap<>();
                map.put("token", loginUser.token);
                map.put("gift_id", id);
                HttpCom.POST(nnhandler, HttpCom.getLingQuUrl(), map, false);
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    /**
     * 礼包详情
     */
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    Log.e("礼包详情json", msg.obj.toString());
                    Gson gson = new Gson();
                    GiftDetBean giftDetBean = gson.fromJson(msg.obj.toString(), GiftDetBean.class);
                    if (giftDetBean != null) {
                        if (giftDetBean.getStatus() == 1) {
                            GiftDetBean.DataBean data = giftDetBean.getData();
                            MCUtils.fillImage(icon, data.getIcon());
                            shuliang.setText("剩余数量：" + data.getNum());
                            if (data.getEnd_time().equals("0")) {
                                riqi.setText("有效日期：永久有效");
                            } else {
                                riqi.setText("有效日期：" + MCUtils.getDataYMD(data.getEnd_time()));
                            }
                            String jieQu = Utils.getJieQu(data.getGiftbag_name());
                            setHeaderTitle(jieQu);
                            giftCont.setText(data.getDesribe());
                            shuming.setText(data.getDigest());
                            qq.setText("客服QQ：" + data.getQq());
                        } else {
                            ToastUtil.showToast(giftDetBean.getReturn_code() + "");
                        }
                    }
                    break;
                case 2:
                    ToastUtil.showToast("网络异常");
                    break;
            }
        }
    };


    /**
     * 领取激活码
     */
    Handler nnhandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    String a = HttpUtils.DNSLingQu(msg.obj.toString());
                    if (a != null) {
                        final GiftDialog millionDialog = new GiftDialog(GiftDetActivity.this, R.style.MillionDialogStyle);
                        millionDialog.setTitle(a);
                        millionDialog.show();
                    }
                    break;
                case 2:
                    ToastUtil.showToast("网络异常");
                    break;
            }
        }
    };
}
